--[[
Baggins_Usable

Author: Daphne Pfister <tiiroib02@sneakemail.com>

Thanks to Nargiddley for writing Baggins

Some of the tooltip filtering code based on the logic used from
ItemRack by Gello.

License

Copyright (c) 2007 Daphne Pfister

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


TODO:
Localize
Consider some sort of table compositing... should not be necassary though sets only time
tables are created/removed enmase is when a new set is added, or an old set is removed.

]]

Baggins_Usable = AceLibrary("AceAddon-2.0"):new("AceConsole-2.0","AceEvent-2.0")

local Baggins_Usable = Baggins_Usable

local dewdrop = AceLibrary:HasInstance("Dewdrop-2.0") and AceLibrary("Dewdrop-2.0")
local tooltip
local llines = {}
local rlines = {}
local DURABILITY_PATTERN = string.match(DURABILITY_TEMPLATE,"(.+) .+/.+") or ""

local function CreateTooltip()
	if tooltip then return end

	tooltip = CreateFrame("GameTooltip")
	tooltip:SetOwner(UIParent, "ANCHOR_NONE")
	for i=1,30 do
		local left = tooltip:CreateFontString()
		local right = tooltip:CreateFontString()
		llines[i] = left
		rlines[i] = right
		left:SetFontObject(GameFontNormal)
		right:SetFontObject(GameFontNormal)
		tooltip:AddFontStrings(left,right)
	end
end

local function ClearTooltip()
	if not tooltip then return CreateTooltip() end

	tooltip:ClearLines()
	for i=1,30 do
		local left = llines[i]
		local right = rlines[i]
		right:SetText()
		left:SetTextColor(0,0,0)
		right:SetTextColor(0,0,0)
	end
	if not tooltip:IsOwned(UIParent) then
		tooltip:SetOwner(UIParent, "ANCHOR_NONE")
	end
end

local function TooltipSetHyperlink(link)
	ClearTooltip()
	tooltip:SetHyperlink(link)
end

local function Matches(bag,slot,rule)
	local link = GetContainerItemLink(bag, slot)
	if not link then return end

	local minLevel = select(5, GetItemInfo(link))
	if minLevel and minLevel > UnitLevel("player") then return end

	local test = rule.test
	if not test then return end

	if test == "Use" then
		if not IsUsableItem(link) then return end
	elseif test == "Consume" then
		if not IsConsumableItem(link) then return end
	elseif test == "Equip" then
		if not IsEquippableItem(link) then return end
	end

	-- Check if something else is preventing item from being used.
	local name = select(1, GetItemInfo(link))
	TooltipSetHyperlink(link)
	local lines = tooltip:NumLines()
	local r,g,b
	for i=2,lines do
		local is_red = nil
		r,g,b = llines[i]:GetTextColor()
		if r>.9 and g<.2 and b<.2 then is_red = true end
		r,g,b = rlines[i]:GetTextColor()
		if r>.9 and g<.2 and b<.2 then is_red = true end
		if is_red then
			local text = llines[i]:GetText()
			if not text:find(DURABILITY_PATTERN) then
				return nil
			end
		end
	end
	return true
end

local function GetName(rule)
	local test = rule.test or "???"
	if test == "Use" then
		return "Usable"
	elseif test == "Consume" then
		return "Consumable"
	elseif test == "Equip" then
		return "Equippable"
	else
		return "Usable:???"
	end
end

local function CleanRule(rule)
	rule.test = "Use"
end

local function DewDropOptions(rule, level, value)
	if level == 1 then
		dewdrop:AddLine("text", "Test", "isTitle", true)
		dewdrop:AddLine("text", "Usable", "checked", rule.test == "Use","func",function(k) rule.test = "Use" Baggins:OnRuleChanged() end,"arg1","Use")
		dewdrop:AddLine("text", "Consumable", "checked", rule.test == "Consume","func",function(k) rule.test = "Consume" Baggins:OnRuleChanged() end,"arg1","Consume")
		dewdrop:AddLine("text", "Equippable", "checked", rule.test == "Equip","func",function(k) rule.test = "Equip" Baggins:OnRuleChanged() end,"arg1","Equip")
	end
end

function Baggins_Usable:OnInitialize()
	Baggins:AddCustomRule("Usable", {
		DisplayName = "Usable",
		Description = "Filter by Usable, Consumable, or Equippable",
		Matches = Matches,
		GetName = GetName,
		CleanRule = CleanRule,
		DewDropOptions = DewDropOptions
	})
end
